<?php

namespace app\common\library;

/**
 * 工具方法
 */
class Tools {
	/**
	 * 获取用户唯一订单号
	 *
	 * @param int $uid 用户ID
	 *
	 * @return string
	 */
	public static function getOrderNo( $uid ) {
		list( $msec, $sec ) = explode( ' ', microtime() );
		$msectime = (float) sprintf( '%.0f', ( floatval( $msec ) + floatval( $sec ) ) * 1000 );

		return $msectime .
			sprintf( '%03d', (int) $uid % 1000 ) .
			str_pad( mt_rand( 1, 999999 ), 6, '0', STR_PAD_LEFT );
	}

	/**
	 * 处理富文本
	 *
	 * @param string $activity 富文本内容
	 *
	 * @return string
	 */
	public static function set_img_style( $activity ) {
		$activity = htmlspecialchars_decode( $activity );//处理富文本标签
		$activity = preg_replace( "/width: [0-9]*px;/", "max-width:100%;", $activity );
		$activity = preg_replace( "/width=\"[0-9]*\"/", "", $activity );
		$activity = str_replace( "<img", "<img style='max-width:100%;height:auto;'", $activity );
		return str_replace( "style=\"", "style=\"width:100%;", $activity );
	}

	/**
	 * 生成二维码
	 *
	 */
	public static function qr_pic($invitor_id){
		//生成二维码:text字段填写要跳转的地址+自己的参数
		$params = [
			'text'       => config("gzh.domain").'pages/login/login?id=' . $invitor_id,
			'size'       => 200,
			'padding'    => 1,
			'errorlevel' => 'medium',
			'foreground' => "#000000",
			'background' => "#ffffff",
		];
		$qrCode = \addons\qrcode\library\Service::qrcode( $params );

		// 写入到文件
		$fileName = '/uploads/qrcode/' . md5( $invitor_id . mt_rand( 10000, 99999 ) ) . '.png';

		if ( !file_exists( ROOT_PATH . 'public/uploads/qrcode/' ) ) {
			mkdir( ROOT_PATH . 'public/uploads/qrcode/', 0777, true );
		}

		$filePath = ROOT_PATH . 'public' . $fileName;
		$qrCode->writeFile( $filePath );

		return $fileName;
	}
}
